package br.com.ca5.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import br.com.ca5.entity.Documento;


public class DocumentoDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DocumentoDAO.class);
	// property constants
	public static final String PATH = "path";
	public static final String TITULO = "titulo";
	public static final String MENU_ID = "menuId";
	public static final String USUARIO_ID = "usuarioId";

	protected void initDao() {
		// do nothing
	}

	public void save(Documento transientInstance) {
		log.debug("saving Documento instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Documento persistentInstance) {
		log.debug("deleting Documento instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Documento findById(java.lang.Long id) {
		log.debug("getting Documento instance with id: " + id);
		try {
			Documento instance = (Documento) getHibernateTemplate().get(
					"br.com.ca5.dao.impl.Documento", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Documento instance) {
		log.debug("finding Documento instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Documento instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Documento as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPath(Object path) {
		return findByProperty(PATH, path);
	}

	public List findByTitulo(Object titulo) {
		return findByProperty(TITULO, titulo);
	}

	public List findByMenuId(Object menuId) {
		return findByProperty(MENU_ID, menuId);
	}

	public List findByUsuarioId(Object usuarioId) {
		return findByProperty(USUARIO_ID, usuarioId);
	}

	public List findAll() {
		log.debug("finding all Documento instances");
		try {
			String queryString = "from Documento";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Documento merge(Documento detachedInstance) {
		log.debug("merging Documento instance");
		try {
			Documento result = (Documento) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Documento instance) {
		log.debug("attaching dirty Documento instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Documento instance) {
		log.debug("attaching clean Documento instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DocumentoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (DocumentoDAO) ctx.getBean("DocumentoDAO");
	}
}